﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaskelot.Services.Model;

namespace Stockholm.SOA.Kaskelot.WCF.ServiceImplementation.Mapping
{
    public static class FaltExtensions
    {
        public static DataContracts.Falt ToContract(this Falt entity)
        {
            DataContracts.Falt f = new DataContracts.Falt();

            CopyFromEntity(entity, f, false);

            return f;
        }

        public static DataContracts.Falt ToMinimalContract(this Falt entity)
        {
            DataContracts.Falt f = new DataContracts.Falt();

            CopyFromEntity(entity, f, true);

            return f;
        }

        public static Falt ToEntity(this DataContracts.Falt f)
        {
            Falt entity = new Falt();

            CopyToEntity(f, entity);

            return entity;
        }

        public static void UpdateEntity(this Falt entity, DataContracts.Falt falt)
        {
            CopyToEntity(falt, entity);
        }

        private static void CopyFromEntity(Falt from, DataContracts.Falt to, bool minimal)
        {
            to.Id = from.Id;
            to.ValId = from.ValId;
            to.Namn = from.Namn;
            to.Typ = (DataContracts.FaltTyp)Enum.Parse(typeof(DataContracts.FaltTyp), from.Typ);
            to.Default = from.Default;
            to.Required = from.Required;
        }

        private static void CopyToEntity(DataContracts.Falt from, Falt to)
        {
            to.Id = from.Id;
            to.ValId = from.ValId;
            to.Namn = from.Namn;
            to.Typ = from.Typ.ToString();
            to.Default = from.Default;
            to.Required = from.Required;
        }
    }
}
